
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '我的客户',
    currentIndex: 0,
    t:'',
    show:false,
    topData: {},
    pubTopData: {},
    xfPubTopData: {},
    title1: "等级",
    title2: "类型",
    title3: "区域",
    title4: "复访",
    option1: [],
    option2: [],
    option3: [],
    option4: [
      { text: '全部', value: '复访', key: '' },
      { text: '首访', value: '首访', key: 'first_inspect' },
      { text: '二访', value: '二访', key: 'second_inspect' },
      { text: '三访', value: '三访', key: 'third_inspect' },
      { text: '多访', value: '多访', key: 'many_inspec' },
    ],
    form: {
      requirement: '',
      level: '',
      location: '',
      repeat_inspect: '',
      keywords: '',
      date:''
    },
    bot:false,
    tabs: ['我的客户', '协防客户', '公池客户']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    // this.setData({
    //   title1: "等级",
    //   title2: "类型",
    //   title3: "区域",
    //   title4: "复访",
    //   form: {
    //     requirement: '',
    //     level: '',
    //     location: '',
    //     repeat_inspect: '',
    //     keywords: ''
    //   }
    // })
    // console.log(this.data.form)
    this.getDictionaryData()
    this.getTopData()
    this.getXfPubTopData()
    this.getPublicTopData()
    if (this.data.currentIndex == 0) {
      this.selectComponent('#list').getCustList()
    }
    if (this.data.currentIndex == 1) {
      this.selectComponent('#list').getXfCustList()
    }
    if (this.data.currentIndex == 2) {
      this.selectComponent('#list').getPbCustList()
    }
  },
  onHide: function () {
    // const child = this.selectComponent('#list')
    // child.setData({
    //   deviceList: child.data.deviceList.splice(0,child.data.deviceList.length - 10)
    // })
    // console.log(child.data.deviceList)
  },
  onLoad: function (options) {

  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  handleChange(e) {
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    let key = e.currentTarget.dataset.key
    let value = e.detail
    console.log(key, e)
    switch (key) {
      case 'op1':
        this.setData({
          ...this.data.form,
          'form.level': value == '等级' ? '' : value,
          title1: value
        })
        break;
      case 'op2':
        this.setData({
          ...this.data.form,
          'form.requirement': value == '类型' ? '' : value,
          title2: value
        })
        break;
      case 'op3':
        this.setData({
          ...this.data.form,
          'form.location': value == '区域' ? '' : value,
          title3: value
        })
        break;
      case 'op4':
        this.setData({
          ...this.data.form,
          'form.repeat_inspect': value == '复访' ? '' : value,
          title4: value
        })
        break;
      default:
        break;
    }
    if (this.data.currentIndex == 0) {
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 1) {
      this.selectComponent('#list').getXfCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 2) {
      const child = this.selectComponent('#list')
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  handleSingleChange(e) {
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    let str
    let value = e.detail
    if (value == '首访') {
      str = 'first_inspect'
    } else if (value == '二访') {
      str = 'second_inspect'
    } else if (value == '三访') {
      str = 'third_inspect'
    } else if (value == '多访') {
      str = 'many_inspect'
    } else if (value == '转入') {
      str = 'into'
    } else if (value == '转出') {
      str = 'out'
    } else {
      str = ''
    }
    this.setData({
      ...this.data.form,
      'form.repeat_inspect': str == '复访' ? '' : str,
      title4: value
    })
    if (this.data.currentIndex == 0) {
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 1) {
      let form = Object.assign({}, this.data.form)
      form.help_type = form.repeat_inspect
      delete form.repeat_inspect
      this.selectComponent('#list').getXfCustList(form)
      return
    }
    if (this.data.currentIndex == 2) {
      const child = this.selectComponent('#list')
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  handleSearchList() {
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    if (this.data.currentIndex == 0) {
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 1) {
      this.selectComponent('#list').getXfCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 2) {
      const child = this.selectComponent('#list')
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  handleInputSearch(e) {
    this.setData({
      ...this.data.form,
      'form.keywords': e.detail.value
    })
  },
  handleTabCh(e) {
    // console.log(4343);
    let index = e.currentTarget.dataset.index
    let arr1 = [
      { text: '全部', value: '协访' },
      { text: '转入', value: '转入' },
      { text: '转出', value: '转出' }
    ]
    let arr2 = [
      { text: '全部', value: '复访' },
      { text: '首访', value: '首访' },
      { text: '二访', value: '二访' },
      { text: '三访', value: '三访' },
      { text: '多访', value: '多访' },
    ]
    this.setData({
      currentIndex: index,
      title1: "等级",
      title2: "类型",
      title3: '区域',
      title4: index == 1 ? "协访" : "复访",
      option4: index == 1 ? arr1 : arr2,
      form: {
        requirement: '',
        level: '',
        location: '',
        date:'',
        repeat_inspect: '',
        keywords: ''
      }
    })
    // console.log(this.selectComponent('#calendar'))
    let arr = this.selectComponent('#calendar').data.days
    arr.forEach(item => {
      if(item.id == this.data.t){
        item.className = ''
      }
    });
    this.selectComponent('#calendar').setData({
      days:arr
    })
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    console.log(index);
    if (index == 0) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (index == 1) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getXfCustList()
      this.getXfPubTopData()
      return
    }
    if (index == 2) {
      
      const child = this.selectComponent('#list')
      child.setData({
        see_show: 1
      })
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  toAddPage() {
    wx.navigateTo({
      url: '../edit/index?type=add',
    })
  },
  //字典列表重组
  filterList(arr, type) {
    let list = []
    let obj = {
      text: '',
      value: ''
    }
    for (let index = 0; index < arr.length; index++) {
      obj = {
        text: arr[index],
        value: arr[index]
      }
      list.push(obj)
    }
    list.unshift({ text: '全部', value: type })
    return list
    // console.log(arr)
  },
  //字典接口
  getDictionaryData() {
    let _this = this
    App._post_form('Common/dictionaryData', {
      keys: '["customer_level","area_list","requirement_type"]'
    }, function (result) {
      _this.setData({
        option1: _this.filterList(result.data.customer_level, '等级'),
        option2: _this.filterList(result.data.requirement_type, '类型'),
        option3: _this.filterList(result.data.area_list, '区域')
      })
    });
    console.log()
  },
  // 公池列表头部数据
  getPublicTopData() {
    let _this = this
    App._post_form('Resource/getTopData', {}, function (result) {
      _this.setData({
        pubTopData: result.data
      })
    });
  },
  // 协防列表头部数据
  getXfPubTopData() {
    let _this = this
    console.log('111')
    App._post_form('HelpDefense/helpDefenseTopData', {}, function (result) {
      _this.setData({
        xfPubTopData: result.data
      })
    });
  },
  /**
   * 获取客户页面头部数据
   */
  getTopData() {
    let _this = this
    App._post_form('customer/getTopData', {}, function (result) {
      _this.setData({
        topData: result.data
      })
      console.log(_this.data.topData)
    });
  },
  //日期选择
  onDayClick(event){
    console.log()
    this.setData({ 
      ...this.data.form,
      'form.date':event.detail.id,
      t:event.detail.id
     });
    console.log(event.detail)
  },
  handleSureTime(){
    this.setData({
      show:false
    })
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    if (this.data.currentIndex == 0) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 1) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getXfCustList(this.data.form)
      this.getXfPubTopData()
      return
    }
    if (this.data.currentIndex == 2) {
      child.setData({
        see_show: 1
      })
      const child = this.selectComponent('#list')
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  handleResetTime(){
    let arr = this.selectComponent('#calendar').data.days
    arr.forEach(item => {
      if(item.id == this.data.t){
        item.className = ''
      }
    });
    this.selectComponent('#calendar').setData({
      days:arr
    })
    this.setData({
      show:false,
      ...this.data.form,
      'form.date':''
    })
    const child = this.selectComponent('#list')
    child.setData({
      deviceList: [],
      deviceIndex: 1
    })
    if (this.data.currentIndex == 0) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getCustList(this.data.form)
      return
    }
    if (this.data.currentIndex == 1) {
      child.setData({
        see_show: 0
      })
      this.selectComponent('#list').getXfCustList(this.data.form)
      this.getXfPubTopData()
      return
    } 
    if (this.data.currentIndex == 2) {
      child.setData({
        see_show: 1
      })
      const child = this.selectComponent('#list')
      this.selectComponent('#list').getPbCustList()
      return
    }
  },
  onReachBottom: function () {
    this.selectComponent("#list").reachBottom(this.data.form)
  },
  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },
  /**
     * 下拉刷新
     */
  onPullDownRefresh: function () {
    // 获取首页数据
    this.onShow()
  }
})